<?php

/**
 * techos actions.
 *
 * @package    SistemaWebUnlSenescyt
 * @subpackage techos
 * @author     JFernando
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class techosActions extends sfActions {

    public function executeGraba(sfWebRequest $request) {

        if (isset($_POST['id']) && isset($_POST['firstname']) && isset($_POST['lastname'])) {
            $id = mysql_escape_String($_POST['id']);
            $firstname = mysql_escape_String($_POST['firstname']);
            $lastname = mysql_escape_String($_POST['lastname']);
            $q = Doctrine_Query::create()
                    ->from('techos t')
                    ->where('t.id_tp = ?', $id);
            $pro = $q->fetchOne();
            $pro->setNombre($firstname);
            $pro->setPorcentaje($lastname);
            $pro->save();
        }
    }

    public function executeIndex(sfWebRequest $request) {
        $idp = $request->getParameter('id_pro');
        $this->getUser()->setAttribute('id_pro', $idp);
        $this->techoss = Doctrine::getTable('techos')->createQuery('t')
                ->where('t.id_pro = ?', $idp)
                ->execute();

        $q = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro=?', $idp);
        $this->proyecto = $q->fetchOne();
    }

    public function executeShow(sfWebRequest $request) {
        $this->techos = Doctrine_Core::getTable('techos')->find(array($request->getParameter('id_tp')));
        $this->forward404Unless($this->techos);
    }

    public function executeNew(sfWebRequest $request) {
        $idp = $request->getParameter('id_pro');
        $this->getUser()->setAttribute('id_pro', $idp);
        $this->form = new techosForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new techosForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($techos = Doctrine_Core::getTable('techos')->find(array($request->getParameter('id_tp'))), sprintf('Object techos does not exist (%s).', $request->getParameter('id_tp')));
        $this->form = new techosForm($techos);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($techos = Doctrine_Core::getTable('techos')->find(array($request->getParameter('id_tp'))), sprintf('Object techos does not exist (%s).', $request->getParameter('id_tp')));
        $this->form = new techosForm($techos);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($techos = Doctrine_Core::getTable('techos')->find(array($request->getParameter('id_tp'))), sprintf('Object techos does not exist (%s).', $request->getParameter('id_tp')));
        $aux = $techos->getIdPro();
        $descripciones = Doctrine::getTable('descripciongeneral')->createQuery('d')
                ->where('d.id_tp = ?', $techos->getIdTP())
                ->execute();
        foreach ($descripciones as $des) :
            $valores = Doctrine::getTable('valor')->createQuery('v')
                    ->where('v.id_dg = ?', $des->getIdDg())
                    ->execute();
            foreach ($valores as $val) :
                $val->delete();
            endforeach;
            $des->delete();
        endforeach;
        $techos->delete();
        $this->redirect('techos/index?id_pro=' . $aux);
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $form->getObject()->setIdPro($this->getUser()->getAttribute('id_pro'));
            $techos = $form->save();
            $this->redirect('techos/index?id_pro=' . $techos->getIdPro());
        }
    }

}
